﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Web.Configuration
Partial Class home
    Inherits System.Web.UI.Page
    Protected Sub LinkButton_editPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton_editPage.Click
        Me.LinkButton_editPage.Visible = False
        Me.div_display.Visible = False
        Me.div_edit.Visible = True
    End Sub

    Protected Sub Button_saveEdits_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_saveEdits.Click
        Me.LinkButton_editPage.Visible = True
        Me.div_display.Visible = True
        Me.div_edit.Visible = False
        Dim myConnection = New OleDbConnection()
        myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings("yobboGuitar").ConnectionString
        'update database with revised homepage text
        'declare and instantiate command object
        Dim myCommand = New OleDbCommand("UPDATE homepage SET homePageText=@homePageText, dateUpdated=@dateUpdated WHERE ID=@ID;")
        myCommand.Parameters.AddWithValue("@homePageText", Me.TextBox_homePageText.Text)
        myCommand.Parameters.AddWithValue("@dateUpdated", DateTime.Now.Day & "/" & DateTime.Now.Month & "/" & DateTime.Now.Year)
        myCommand.Parameters.AddWithValue("@ID", CInt(Me.HiddenField_IDHomePageText.Value))
        Try
            'open connection
            myConnection.Open()
            'set connection property of command object
            myCommand.Connection = myConnection
            'execute update query
            myCommand.ExecuteNonQuery()
            'close connection
            myConnection.Close()
        Catch ex As Exception
            'redirect to error message page
            Response.Redirect("~/error.htm")
        Finally
            myConnection.Close()
        End Try
        'reload page from scratch
        Response.Redirect("~/home.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            If util.getUserAccessLevel < 2 Then
                Me.LinkButton_editPage.Visible = False 'hide page edit link if user does not have the appropriate access control level
                Me.LinkButton_siteAdmin.Visible = False 'likewise hide link to admin page
            End If

            'populate page with text from database stored in "homepage" table
            'declare and instantiate connection object - connection string is stored in web.config
            Dim myConnection = New OleDbConnection()
            myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings("yobboGuitar").ConnectionString
            'declare and instantiate command object
            Dim myCommand = New OleDbCommand("SELECT ID, dateUpdated, homePageText FROM homepage;")
            Try
                'open connection
                myConnection.Open()
                'set connection property of command object
                myCommand.Connection = myConnection
                'declare reader
                Dim myReader As OleDbDataReader
                'execute reader
                myReader = myCommand.ExecuteReader
                myReader.Read()
                'populate page with text
                Me.div_text.InnerHtml = myReader("homePageText")
                Me.TextBox_homePageText.Text = myReader("homePageText")
                Me.div_textDate.InnerHtml = "<b>Last updated:</b>&nbsp;" & myReader("dateUpdated")
                Me.HiddenField_IDHomePageText.Value = myReader("ID") 'hidden field used when editing
                'close connection
                myConnection.Close()
            Catch ex As Exception
                'redirect to error message page
                Response.Redirect("~/error.htm")
            Finally
                myConnection.Close()
            End Try


            'declare and instantiate command object
            myCommand = New OleDbCommand("SELECT TOP 10 * FROM songListing ORDER BY date DESC")
            Try
                'open connection
                myConnection.Open()
                'set connection property of command object
                myCommand.Connection = myConnection
                'declare reader
                Dim myReader As OleDbDataReader
                'execute reader
                myReader = myCommand.ExecuteReader
                If myReader.HasRows Then
                    'set data source property of grid view and invoke dataBind method to fill control
                    Me.GridView_results.DataSource = myReader
                    Me.GridView_results.DataBind()
                End If
                'close connection
                myConnection.Close()
            Catch ex As Exception
                'redirect to error message page
                Response.Redirect("~/error.htm")
            Finally
                myConnection.Close()
            End Try


        End If
       
    End Sub

    
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Response.Redirect("~/rss.aspx")
    End Sub
    
    Protected Sub LinkButton_siteAdmin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton_siteAdmin.Click
        Response.Redirect("~/admin.aspx")
    End Sub
End Class
